/*
 * Filename:    TimeCheckServiceImpl.java
 * Copyright:   2015 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2015-3-31
 */
package com.paic.mhis.hcpms.common.time.biz.service.Impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.paic.mhis.hcpms.common.time.biz.service.TimeCheckService;
import com.paic.mhis.hcpms.common.time.dao.TimeCheckDao;
import com.paic.mhis.hcpms.common.time.dto.TimeCheckDTO;

@Component("timeCheckService")
public class TimeCheckServiceImpl implements TimeCheckService {
	@Autowired
	private TimeCheckDao timeCheckDao;

	/** ===============第一种方法===================== */
	@Override
	@Transactional
	public List<TimeCheckDTO> checkTimeOne(String startDate, String endDate, String code) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);

		List<TimeCheckDTO> warnInfos = timeCheckDao.queryWarnInfo(map);
		List<TimeCheckDTO> warnInfoChecks = compareTimeOne(startDate, endDate, warnInfos);

		List<TimeCheckDTO> auditCases = timeCheckDao.queryAuditCase(map);
		List<TimeCheckDTO> auditCasesChecks = compareTimeOne(startDate, endDate, auditCases);
		warnInfoChecks.addAll(auditCasesChecks);
 
		if (null == auditCasesChecks || auditCasesChecks.size() < 1) {
			List<TimeCheckDTO> auditProblems = timeCheckDao.queryAuditProblem(map);
			List<TimeCheckDTO> problemsChecks = compareTimeOne(startDate, endDate, auditProblems);
			warnInfoChecks.addAll(problemsChecks);
		}
		return warnInfoChecks;
	}

	private static List<TimeCheckDTO> compareTimeOne(String startDate, String endDate, List<TimeCheckDTO> list) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		List<TimeCheckDTO> dtos = new ArrayList<TimeCheckDTO>();
		for (TimeCheckDTO checkDTO : list) {
			TimeCheckDTO dto = new TimeCheckDTO();
			try {
				Integer t1 = trimChar(sdf.format(checkDTO.getStartDate()));
				Integer t2 = trimChar(sdf.format(checkDTO.getEndDate()));

				Integer t3 = trimChar(startDate);
				Integer t4 = trimChar(endDate);
				if (t2 < t3 || t4 < t1) {
					continue;
				} else {
					dtos.add(checkDTO);
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return dtos;
	}

	private static Integer trimChar(String str) {
		String newString = str.replaceAll("-", "");
		return Integer.parseInt(newString);
	}

	/** ===============第二种方法===================== */
	@Override
	@Transactional
	public List<TimeCheckDTO> checkTimeTwo(Date startDate, Date endDate, String code) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", code);

		List<TimeCheckDTO> warnInfos = timeCheckDao.queryWarnInfo(map);
		List<TimeCheckDTO> warnInfoChecks = compareTimeTwo(startDate, endDate, warnInfos);

		List<TimeCheckDTO> auditCases = timeCheckDao.queryAuditCase(map);
		List<TimeCheckDTO> auditCasesChecks = compareTimeTwo(startDate, endDate, auditCases);
		warnInfoChecks.addAll(auditCasesChecks);
		if (null == auditCasesChecks || auditCasesChecks.size() < 1) {
			List<TimeCheckDTO> auditProblems = timeCheckDao.queryAuditProblem(map);
			List<TimeCheckDTO> ProblemsChecks = compareTimeTwo(startDate, endDate, auditProblems);
			warnInfoChecks.addAll(ProblemsChecks);
		}
		return warnInfoChecks;
	}

	private static List<TimeCheckDTO> compareTimeTwo(Date startDate, Date endDate, List<TimeCheckDTO> list) {
		List<TimeCheckDTO> dtos = new ArrayList<TimeCheckDTO>();
		for (TimeCheckDTO checkDTO : list) {
			Date b1 = checkDTO.getStartDate();
			Date e1 = checkDTO.getEndDate();
			Date b2 = startDate;
			Date e2 = endDate;
			if ((b2.after(b1) && b2.before(e1)) || (e2.after(b1) && e2.before(e1)) || (b1.before(b2) && e1.after(e2))
					|| (b2.before(b1) && e2.after(e1))) {
				dtos.add(checkDTO);
			}
		}
		return dtos;
	}

	// ====================================

	public static void main(String[] args) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

		// try {
		// TimeCheckDTO dto = new TimeCheckDTO();
		// dto.setStartDate(sdf.parse("2015-01-02"));
		// dto.setEndDate(sdf.parse("2015-02-04"));
		// dto.setCode("001");
		// TimeCheckDTO dto1 = new TimeCheckDTO();
		// dto1.setStartDate(sdf.parse("2015-05-08"));
		// dto1.setEndDate(sdf.parse("2015-06-04"));
		// dto1.setCode("002");
		// List<TimeCheckDTO> list = new ArrayList<TimeCheckDTO>();
		// list.add(dto);
		// list.add(dto1);
		//
		// TimeCheckDTO compareTimeOne = compareTimeOne("2015-03-01",
		// "2015-05-10", dto, list);
		// TimeCheckDTO compareTimeTwo = compareTimeTwo(sdf.parse("2015-03-01"),
		// sdf.parse("2015-05-10"), dto, list);
		//
		// System.out.println("String====" + compareTimeOne.isFlag() +
		// "==Code==" + compareTimeOne.getCode());
		// System.out.println("Date====" + compareTimeTwo.isFlag() + "==Code=" +
		// compareTimeTwo.getCode());
		// } catch (Exception e) {
		// e.printStackTrace();
		// }

		List<String> list = new ArrayList<String>();
		// list.add("aaa");
		// list.add("bbb");
		// List<String> list1 = new ArrayList<String>();
		// list1.add("aaa1");
		// list1.add("bbb1");
		// List<String> list2 = new ArrayList<String>();
		// list2.add("aaa2");
		// list2.add("bbb2");
		// list.addAll(list1);
		// list.addAll(list2);
		System.out.println(list.size() < 1);
	}
}
